import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:url_launcher/url_launcher.dart';

import '../manger/app_info/app_info_manager.dart';
import '../manger/channel/method_channel_manager.dart';
import '../widgets/loading.dart';


class AppUtil {
  // static Future<bool> checkApp(String packageName) async {
  //   return await nativeBridge.checkApp(packageName);
  // }
  //
  // static Future<bool> isRoot() async {
  //   return await nativeBridge.isRoot();
  // }

  /// 打开游戏
  static openGame(dynamic model, int isOpenAppleStore) async {
    if (GetPlatform.isIOS) {
      openIosGame(model, isOpenAppleStore);
    }

    if (GetPlatform.isAndroid) {
      openAndroidGame(model);
    }
  }

  ///ios 打开app
  static openIosGame(dynamic model, int isOpenAppleStore) async {
    if (isOpenAppleStore == 1) {
      Loading.show(dismissOnTap: true);

      ///打开游戏
      final Uri url = Uri.parse(model.schemeUrl ?? "");

      /// 如果安装成功直接打开
      if (await launchUrl(url, mode: LaunchMode.externalApplication)) {
      } else {
        ///没有安装 先打开弹窗
        //int? code = await MethodChannelManager().callNativeOpenAppDownload(model.appleId ?? "");

        ///没有商店跳转网页
        // if (code == null) {
        //   final Uri uri = Uri.parse(model.downloadUrl ?? "");
        //   launchUrl(uri, mode: LaunchMode.externalApplication);
        // }

        ///ad 归因
        ///await Api.deepAd(model.adjustClickUrl!);
      }

      Loading.dismiss();

      return;
    }
   // NavigatorUtil.pushPage(Get.context!, GameInfoPage(model: model));
  }

  ///Android 打开或下载游戏
  static openAndroidGame(dynamic model) async {
    Loading.show(dismissOnTap: true);
    // final userLogic = Get.find<UserProvider>();
    //
    // ///referrer
    // String value =
    //     "${AppInfoManager().info.visitorId}&${AppInfoManager().info.appName}&${AppInfoManager().campaign}&${userLogic.getUId()}&${AppInfoManager().environment}&${AppInfoManager().vifv}&${AppInfoManager().info.appBuildVersion}";
    //
    // Map<String, String> map = {"pgk": model.appleBid ?? "", "referrer": value};
    //
    // MethodChannelManager()
    //     .callNativeMethod(ChannelMethodEnum.openAppDownload, map);
    Loading.dismiss();
  }
}
